﻿// <copyright file="DebugService.cs" company="Microsoft">
// Copyright (c) 2010 Microsoft All Rights Reserved
// </copyright>
// <author>Microsoft</author>
// <email>mischero@microsoft.com</email>
// <date>2010-01-01</date>
// <summary>Web analytics service behavior class definition</summary>

namespace SSMEPlayer
{
    using System.ComponentModel.Composition;
    using System.Windows;
    using System.Windows.Interactivity;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// Track an event to a web analytics service
    /// </summary>
    public class DebugService : Behavior<FrameworkElement>
    {
        #region Properties
        /// <summary>
        /// Gets or sets the events
        /// </summary>
        [Import]
        public WebAnalyticsEvents Events { get; set; }
        #endregion

        #region Methods
        /// <summary>
        /// Initialize the imported parts and attache the TrackEvent handler
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            CompositionInitializerEx.SatisfyImports(this);

            this.Events.TrackEvent += new System.EventHandler<AnalyticsEventArgs>(this.OnTrackEvent);
        }

        /// <summary>
        /// Detach the TrackEvent() handler
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.Events.TrackEvent -= new System.EventHandler<AnalyticsEventArgs>(this.OnTrackEvent);
        }
        #endregion

        #region Implementation
        /// <summary>
        /// Track the event to the service
        /// </summary>
        /// <param name="sender">the event tracker</param>
        /// <param name="e">the anlalytics event arguments</param>
        private void OnTrackEvent(object sender, AnalyticsEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.AnalyticsEvent.Name);
        }
        #endregion
    }
}
